﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 必须是枚举字符串, 示例:
/// <code>
/// [VMustBeEnum&lt;EnumStatus?>(isStrict: true)]
/// public string Status { get; set; }
/// </code>
/// </summary>
/// <remarks>
/// 注意: 
/// <list type="bullet">
/// <item>当属性不是字符串时, 会先调用 ToString() 转为字符串再进行判断, 这可以用于对非字符串类型的校验</item>
/// <item>当参数 isStrict 为false时, 效果等同 <seealso cref="VMustCanConvertToAttribute{T}"/></item>
/// <item>当参数 isStrict 为true时, 表示  Enum.Parse(原始字符串).ToString() == 原始字符串</item>
/// </list> 
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustBeEnumAttribute<T>(bool isStrict = false, string errorMessage = null) : Attribute, IValidateAction
{
    public bool IsStrict { get; } = isStrict;
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustBeEnumInternal<T>(ctx, ctx.ModelValue?.ToString(), IsStrict, ErrorMessage);
    }
}
